import logging
import os
from logging.config import dictConfig


class LogHandler(logging.Logger):
    runPath = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

    logPath = os.path.join(runPath, "log")

    if not os.path.exists(logPath):
        os.makedirs(logPath)

    logDict = {
        "version": 1,
        "formatters": {
            "console_formatter": {"format": "%(asctime)s - %(message)s", },
            "file_formatter": {
                "format": "%(asctime)s - %(name)-8s - %(levelname)-8s - %(message)s",
            },
        },
        "handlers": {
            "consoleHandler": {
                "class": "logging.StreamHandler",
                "level": "INFO",
                "stream": "ext://sys.stdout",
                "formatter": "console_formatter",
            },
            "rotHandler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "DEBUG",
                "formatter": "file_formatter",
                "filename": "{}".format(
                    os.path.join(logPath, "update_populate.log")
                ),
                "mode": "a",
                "maxBytes": 10240000,
                "backupCount": 5,
            },
        },
        "root": {"level": "DEBUG", "handlers": ["consoleHandler", "rotHandler"], },
    }

    dictConfig(logDict)

    def __init__(self, name, level=logging.NOTSET):

        super().__init__(name, level)
